/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.app.*
import com.almasb.fxgl.core.collection.*
import com.almasb.fxgl.core.math.*
import com.almasb.fxgl.dsl.*
import javafx.scene.input.*
import javafx.scene.paint.*

/**
 * Shows how to use global property variables.
 *
 * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
 */
class VarsSample : GameApplication() {
	override fun initSettings(settings: GameSettings) {
		settings.width = 800
		settings.height = 600
		settings.title = "VarsSample"
	}
	
	override fun initInput() {
		onKeyDown(KeyCode.Q, "boolean") { set("testBoolean", !getb("testBoolean")) }
		
		onKeyDown(KeyCode.W, "int") { inc("lives", +2) }
		
		onKeyDown(KeyCode.E, "double") { inc("testDouble", +1.5) }
	}
	
	override fun initGameVars(vars: MutableMap<String, Any>) {
		vars["testDouble"] = -1.5
		vars["testBoolean"] = true
		vars["vector"] = Vec2(1f, 1f)
		
		vars["score"] = 0
		vars["lives"] = 3
	}
	
	override fun initGame() {
		//NOTE 为什么必须这样？
		getGameState().addListener("vector", object : PropertyChangeListener<Vec2> {
			override fun onChange(prev: Vec2, now: Vec2) {
				println("$prev $now")
			}
		})
		
		println(getGameState().getDouble("testDouble"))
		
		println(getGameState().getBoolean("testBoolean"))
		
		println(getGameState().getObject<Vec2>("vector").x)
		
		println(getGameState().objectProperty<Vec2>("vector").get().y)
		
		getGameState().setValue("vector", Vec2(300f, 300f))
	}
	
	override fun initUI() {
		val uiScore = getUIFactory().newText("", Color.BLACK, 16.0)
		uiScore.translateX = 100.0
		uiScore.translateY = 100.0
		uiScore.textProperty().bind(getip("score").asString())
		
		getGameScene().addUINode(uiScore)
	}
	
	override fun onUpdate(tpf: Double) {
		inc("score", +1)
	}
}

fun main(args: Array<String>) {
	GameApplication.launch(VarsSample::class.java, args)
}
